﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter23.ViewModel
{
    using System.Globalization;
    using System.Windows;
    using Microsoft.Win32;

    public class WindowPositionHelper
    {
        public static string RegPath = @"Software\MyApp\WindowBounds\";

        public static void SvaeSize(Window win)
        {
            RegistryKey key;
            key = Registry.CurrentUser.CreateSubKey(RegPath + win.Name);
            key.SetValue("Bounds", win.RestoreBounds.ToString());
            win.RestoreBounds.ToString(CultureInfo.InvariantCulture);
        }

        public static void SetSize(Window win)
        {
            RegistryKey key;
            key = Registry.CurrentUser.OpenSubKey(RegPath + win.Name);
            if (key != null)
            {
                var bounds = Rect.Parse(key.GetValue("Bounds").ToString());
                win.Top = bounds.Top;
                win.Left = bounds.Left;
                if (win.SizeToContent == SizeToContent.Manual)
                {
                    win.Width = bounds.Width;
                    win.Height = bounds.Height;
                }
            }
        }
    }
}
